package com.qkl.qkl.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author tianhaoyi
 * @date 2017年3月27日
 * @version 1.0
 */
public class DateUtil {
	private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

	private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");

	private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");

	private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 获取YYYY格式
	 * 
	 * @return
	 */
	public static String getYear() {
		return sdfYear.format(new Date());
	}

	/**
	 * 获取YYYY-MM-DD格式
	 * 
	 * @return
	 */
	public static String getDay() {
		return sdfDay.format(new Date());
	}

	/**
	 * 获取YYYYMMDD格式
	 * 
	 * @return
	 */
	public static String getDays() {
		return sdfDays.format(new Date());
	}

	/**
	 * 获取YYYY-MM-DD HH:mm:ss格式
	 * 
	 * @return
	 */
	public static String getTime() {
		return sdfTime.format(new Date());
	}

	/**
	 * @Title: compareDate
	 * @Description: TODO(日期比较，如果s>=e 返回true 否则返回false)
	 * @param s
	 * @param e
	 * @return boolean
	 * @throws @author
	 *             luguosui
	 */
	public static boolean compareDate(String s, String e) {
		if (fomatDate(s) == null || fomatDate(e) == null) {
			return false;
		}
		return fomatDate(s).getTime() >= fomatDate(e).getTime();
	}

	/**
	 * 格式化日期
	 * 
	 * @return
	 */
	public static Date fomatDate(String date) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return fmt.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 校验日期是否合法
	 * 
	 * @return
	 */
	public static boolean isValidDate(String s) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			fmt.parse(s);
			return true;
		} catch (Exception e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return false;
		}
	}

	public static int getDiffYear(String startTime, String endTime) {
		DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			long aa = 0;
			int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(startTime).getTime()) / (1000 * 60 * 60 * 24))
					/ 365);
			return years;
		} catch (Exception e) {
			// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
			return 0;
		}
	}

	/**
	 * <li>功能描述：时间相减得到天数
	 * 
	 * @param beginDateStr
	 * @param endDateStr
	 * @return long
	 * @author Administrator
	 */
	public static long getDaySub(String beginDateStr, String endDateStr) {
		long day = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date beginDate = null;
		Date endDate = null;

		try {
			beginDate = format.parse(beginDateStr);
			endDate = format.parse(endDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
		// System.out.println("相隔的天数="+day);

		return day;
	}

	/**
	 * 得到n天之后的日期
	 * 
	 * @param days
	 * @return
	 */
	public static String getAfterDayDate(String days) {
		int daysInt = Integer.parseInt(days);

		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();

		SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdfd.format(date);

		return dateStr;
	}

	/**
	 * 得到n天之后是周几
	 * 
	 * @param days
	 * @return
	 */
	public static String getAfterDayWeek(String days) {
		int daysInt = Integer.parseInt(days);

		Calendar canlendar = Calendar.getInstance(); // java.util包
		canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
		Date date = canlendar.getTime();

		SimpleDateFormat sdf = new SimpleDateFormat("E");
		String dateStr = sdf.format(date);

		return dateStr;
	}

	// 把String转换成Date如：2010-01-07 13:10:10,yyyy-MM-dd HH:mm:ss
	public static Date changeStringToDate(String time, String fomat) {
		Date t = null;
		if (time != null && !time.trim().equals("")) {
			try {
				SimpleDateFormat formatter = new SimpleDateFormat(fomat);
				t = formatter.parse(time);
			} catch (ParseException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		return t;
	}

	// 把Date转换成String如：2010-01-07 13:10:10
	public static String dateToString(Date time, String fomat) {
		String ctime = "";
		SimpleDateFormat formatter = new SimpleDateFormat(fomat);
		if (time != null) {
			ctime = formatter.format(time);
		}
		return ctime;
	}

	/**
	 * 根据日期，获取本周时间
	 * 
	 * @return
	 */
	public static String[] getWeekDay(Date nowDate) {
		String[] weekDay = new String[7];

		Calendar calendar1 = Calendar.getInstance();
		calendar1.setTime(nowDate);

		int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK)-1 ;
		

		if(dayOfWeek==0){
			dayOfWeek = dayOfWeek+7;
		}
		// 星期一的日期
		int offset1 = 1 - dayOfWeek;

		calendar1.add(calendar1.DATE, offset1);

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		weekDay[0] = sdf.format(calendar1.getTime());

		// 星期二的日期
		Calendar calendar2 = Calendar.getInstance();
		calendar2.setTime(nowDate);

		int offset2 = 2 - dayOfWeek;

		calendar2.add(calendar2.DATE, offset2);

		weekDay[1] = sdf.format(calendar2.getTime());
		// 星期三的日期
		Calendar calendar3 = Calendar.getInstance();
		calendar3.setTime(nowDate);
		
		int offset3 = 3 - dayOfWeek;

		calendar3.add(calendar3.DATE, offset3);

		weekDay[2] = sdf.format(calendar3.getTime());
		// 星期四的日期
		Calendar calendar4 = Calendar.getInstance();
		calendar4.setTime(nowDate);
		
		int offset4 = 4 - dayOfWeek;

		calendar4.add(calendar4.DATE, offset4);

		weekDay[3] = sdf.format(calendar4.getTime());
		// 星期五的日期
		Calendar calendar5 = Calendar.getInstance();
		calendar5.setTime(nowDate);
		int offset5 = 5 - dayOfWeek;

		calendar5.add(calendar5.DATE, offset5);

		weekDay[4] = sdf.format(calendar5.getTime());
		// 星期六的日期
		Calendar calendar6 = Calendar.getInstance();
		calendar6.setTime(nowDate);
		int offset6 = 6 - dayOfWeek;

		calendar6.add(calendar6.DATE, offset6);

		weekDay[5] = sdf.format(calendar6.getTime());
		// 星期日的日期
		Calendar calendar7 = Calendar.getInstance();
		calendar7.setTime(nowDate);
		
		int offset7 = 7 - dayOfWeek;

		calendar7.add(calendar7.DATE, offset7);

		weekDay[6] = sdf.format(calendar7.getTime());

		return weekDay;
	}


	/**
	 * 得到32位的UUID
	 *
	 * @return
	 */
	public static String getUUID() {
		String uuid = java.util.UUID.randomUUID().toString().replaceAll("-", "");
		return uuid;
	}
}